"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 系统内部使用工具
Case Name   : pssh指定-e并指定不存在的错误信息目录
Create At   : 2022/4/24
@zou_jialiang0501129488
Description :
    1.pssh指定-e并指定不存在的错误信息目录
    2.清理环境
Expect      :
    1.系统将错误结果保存至该文件夹中
    2.清理成功
History     :
"""
import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.Common import CommonSH
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2不执行')
class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_root_node = Node('PrimaryRoot')
        self.s_root_node1 = Node('Standby1Root')
        self.s_root_node2 = Node('Standby2Root')
        self.s_node1 = Node("Standby1DbUser")
        self.s_node2 = Node("Standby2DbUser")
        self.com_root = CommonSH("PrimaryRoot")
        self.common = Common()
        self.dir_path = os.path.join(os.path.dirname(macro.DB_INSTANCE_PATH),
                                     'not_exists_path')
        self.ssh_file = os.path.join('~', '.ssh', '*')

    def test_pssh(self):
        text = '-----step1:pssh指定-e并指定不存在的错误信息目录; expect:成功-----'
        self.log.info(text)
        self.log.info("-----创建root互信-----")
        global err_path
        result = self.common.get_sh_result(self.primary_root_node,
                                           f"ls {macro.DB_SCRIPT_PATH}")
        if "gs_sshexkey" not in result:
            cmd = f"cd {macro.DB_SCRIPT_PATH}/../; " \
                  f"tar -zxvf openGauss-Package-bak*.tar.gz > /dev/null"
            self.common.get_sh_result(self.primary_root_node, cmd)
            result = self.common.get_sh_result(self.primary_root_node,
                                               f"ls {macro.DB_SCRIPT_PATH}")
            self.assertIn('gs_sshexkey', result, '文件传输失败')

        self.host_tuple = (self.primary_root_node.ssh_host,
                           self.s_node1.ssh_host, self.s_node2.ssh_host)
        self.params = {'-f': 'test_hosts'}
        self.com_root.exec_gs_sshexkey(macro.DB_SCRIPT_PATH,
                                       *self.host_tuple,
                                       **self.params)
        test = '-----查看主机名-----'
        self.log.info(test)
        node_name = self.common.get_sh_result(self.s_root_node1, "uname -n")
        self.assertIsNotNone(node_name, '执行失败:' + text)
        self.log.info('-----执行pssh命令-----')
        pssh_cmd = f"pssh -s -e {self.dir_path} -H {node_name} asdf"
        self.log.info(pssh_cmd)
        pssh_res = self.common.get_sh_result(self.primary_root_node, pssh_cmd)
        self.assertIn('command not found', pssh_res, '执行失败')
        err_path = os.path.join(self.dir_path, f'{node_name}')
        check_cmd = f"cat {err_path}"
        self.log.info(check_cmd)
        check_res = self.common.get_sh_result(self.primary_root_node, check_cmd)
        self.assertIn('command not found', check_res, '执行失败')

    def tearDown(self):
        text = f'-----step2:清理环境 expect:成功-----'
        self.log.info(text)
        del_res = self.common.get_sh_result(self.primary_root_node,
                                            f'rm -rf {self.dir_path}')
        del_cmd = f'rm -rf {self.ssh_file}'
        self.log.info(del_cmd)
        del_res1 = self.common.get_sh_result(self.primary_root_node, del_cmd)
        del_res2 = self.common.get_sh_result(self.s_root_node1, del_cmd)
        del_res3 = self.common.get_sh_result(self.s_root_node2, del_cmd)
        self.assertEqual('', del_res, '互信删除失败')
        self.assertEqual('', del_res1, '互信删除失败')
        self.assertEqual('', del_res2, '互信删除失败')
        self.assertEqual('', del_res3, '文件删除失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
